local joy__pinting = fk.CreateSkill {

  name = "joy__pinting",

  tags = {  },

}



joy__pinting:addEffect(fk.EventPhaseStart, {
  name = "joy__pinting",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(joy__pinting.name) and target == player and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 2 + player:getMark("joy__shuangshu_pt-turn")
    local all_choices = {}
    for i = 1, 4 do
      table.insert(all_choices, "joy__pinting_choice"..i)
    end
    local choices = room:askForChoices(player, all_choices, 1, n, joy__pinting.name, "#joy__pinting-choice:::"..n)
    local list = {}
    for i = 1, 4 do
      if table.contains(choices, "joy__pinting_choice"..i) then
        table.insert(list, i)
      end
    end
    room:setPlayerMark(player, "@joy__pinting_choices-phase", table.concat(list, "-"))
    if table.contains(list, 1) and #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      return e.data.from == player
    end, Player.HistoryPhase) == 0 then
      room:setPlayerMark(player, "joy__pinting_tmd-phase", 1)
    end
  end,
})
joy__pinting:addEffect(fk.AfterCardUseDeclared, {
  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("joy__pinting_tmd-phase") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "joy__pinting_tmd-phase", 0)
  end,
})

joy__pinting:addEffect(fk.CardUsing, {
  name = "#joy__pinting_delay",
  events = {fk.CardUsing, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    local mark = player:getMark("@joy__pinting_choices-phase")
    if target == player and mark ~= 0 then
      local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 4, function(e)
        return e.data.from == player
      end, Player.HistoryPhase)
      if event == fk.CardUseFinished then
        if string.find(mark, "3") and events[3] and events[3].data == data then
          event:setCostData(self, 3)
          return true
        end
      else
        if string.find(mark, "2") and events[2] and events[2].data == data then
          local place = player.room:getCardArea(data.card)
          if place == Card.Processing or place == Card.PlayerEquip or place == Card.PlayerJudge then
            event:setCostData(self, 2)
            return true
          end
        elseif string.find(mark, "4") and events[4] and events[4].data == data then
          event:setCostData(self, 4)
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) == 2 then
      room:moveCardTo(data.card, Card.PlayerHand, player, fk.ReasonPrey, "joy__pinting")
    elseif event:getCostData(self) == 3 then
      player:drawCards(2, "joy__pinting")
    else
      local name = Fk:cloneCard(data.card.name)
      local use ={
        from = player.id,
        tos = data.tos,
        card = name,
        extraUse = true
      }
      room:useCard(use)
    end
  end,
})
joy__pinting:addEffect(fk.CardUseFinished, {
  name = "#joy__pinting_delay",
  events = {fk.CardUsing, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    local mark = player:getMark("@joy__pinting_choices-phase")
    if target == player and mark ~= 0 then
      local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 4, function(e)
        return e.data.from == player
      end, Player.HistoryPhase)
      if event == fk.CardUseFinished then
        if string.find(mark, "3") and events[3] and events[3].data == data then
          event:setCostData(self, 3)
          return true
        end
      else
        if string.find(mark, "2") and events[2] and events[2].data == data then
          local place = player.room:getCardArea(data.card)
          if place == Card.Processing or place == Card.PlayerEquip or place == Card.PlayerJudge then
            event:setCostData(self, 2)
            return true
          end
        elseif string.find(mark, "4") and events[4] and events[4].data == data then
          event:setCostData(self, 4)
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) == 2 then
      room:moveCardTo(data.card, Card.PlayerHand, player, fk.ReasonPrey, "joy__pinting")
    elseif event:getCostData(self) == 3 then
      player:drawCards(2, "joy__pinting")
    else
      local name = Fk:cloneCard(data.card.name)
      local use ={
        from = player.id,
        tos = data.tos,
        card = name,
        extraUse = true
      }
      room:useCard(use)
    end
  end,
})

joy__pinting:addEffect("targetmod", {
  name = "#joy__pinting_targetmod",
  bypass_distances = function(self, player)
    return player:getMark("joy__pinting_tmd-phase") > 0
  end,
})

return joy__pinting